import time

from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QGroupBox
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QDialog
from PyQt5.Qt import QColor
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import QThread
from PyQt5 import QtCore
import os
import time
from PHAnalysisApp import IpaAppAnalysis


class UpdateUi(QThread):
    breakSignal = pyqtSignal(str)

    def __init__(self, path, password, parent=None):
        super().__init__(parent)
        self.path = path
        if password:
            self.password = password
        else:
            self.password ='alpine'

    def run(self):
        try:

            ipa = IpaAppAnalysis(self.path)  #'/Users/libo/Desktop/三晋先锋.ipa'
            ipa.set_device_password(self.password)
            result = ipa.analysis()
            time.sleep(2)
            for key, value in result.items():
                self.breakSignal.emit(key + ' ' + value)
        except Exception as e:
            print(e)


class ApplePasswordDialog(QDialog):
    def __init__(self):
        super(QDialog, self).__init__()
        self.text = ''
        self.resize(350, 100)
        self.setWindowTitle("密码输入框")

        self.lb = QLabel("请输入苹果手机设备密码(使用默认密码请取消)：", self)

        self.edit = QLineEdit(self)
        self.edit.installEventFilter(self)

        self.bt1 = QPushButton("确定", self)
        self.bt2 = QPushButton("取消", self)

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(self.bt1)
        hbox.addStretch(1)
        hbox.addWidget(self.bt2)
        hbox.addStretch(1)

        vbox = QVBoxLayout()
        vbox.addWidget(self.lb)
        vbox.addWidget(self.edit)
        vbox.addStretch(1)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

        self.bt1.clicked.connect(self.ok)
        self.bt2.clicked.connect(self.cancel)

    def ok(self):
        self.text = self.edit.text()
        self.done(0)

    def cancel(self):
        self.done(0)


class AnalysisAppWidget(QWidget):
    thread = None

    def __init__(self, log_out):
        super(QWidget, self).__init__()

        file_type_combo = QComboBox()
        file_type_combo.addItem('ipa')
        file_type_combo.addItem('apk')

        file_path_label = QLabel('文件路径: ')
        self.file_path_text = QLineEdit()

        search_button = QPushButton('选择文件')
        search_button.clicked.connect(self.select_file)

        test_button = QPushButton('测试')
        test_button.clicked.connect(self.test)

        ctrl_h_box = QHBoxLayout()
        ctrl_h_box.addWidget(file_type_combo)
        ctrl_h_box.addWidget(file_path_label)
        ctrl_h_box.addWidget(self.file_path_text)
        ctrl_h_box.addWidget(search_button)
        ctrl_h_box.addWidget(test_button)

        self.text = QTextEdit()
        self.text.setFontPointSize(13)
        self.text.setTextColor(QColor(0, 0, 255))
        content_h_box = QHBoxLayout()
        content_h_box.addWidget(self.text)

        temp_group_box = QGroupBox('结果展示')
        temp_group_box.setLayout(content_h_box)

        h_text_box = QHBoxLayout()
        h_text_box.addWidget(temp_group_box)

        context_v_box = QVBoxLayout()
        context_v_box.addLayout(ctrl_h_box)
        context_v_box.addLayout(h_text_box)

        self.setLayout(context_v_box)
        self.setAcceptDrops(True)

    def select_file(self):
        file_path, fileType = QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),
                                                                   "All Files(*);;ipa Files(*.ipa);;apk Files(*.apk)")
        self.file_path_text.setText(file_path)

    def test(self):
        try:

            pwd = ApplePasswordDialog()
            has = pwd.exec_()
            print(pwd.text)

            if not self.file_path_text.text():
                return
            self.text.clear()
            self.text.setText('loading...')
            self.thread = UpdateUi(self.file_path_text.text(), pwd.text)
            self.thread.breakSignal.connect(self.update_text_edit)
            self.thread.start()
        except Exception as e:
            print(e)

    def update_text_edit(self, text):
        try:
            self.text.append(text)
        except Exception as e:
            print(e)

    def dragEnterEvent(self, e):
        if e.mimeData().hasText():
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        text = e.mimeData().text()
        self.file_path_text.setText(text[7:])

